<?php
 /**
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   *
   * ----------------------------------------------------------------------
   * NimWiki - designer centric wiki
   *
   * @author Nicolas Myers <myers.nicolas@gmail.com>
   * @copyright 2007 Nicolas Myers
   * @version 0.9.0
   * @link
   * ----------------------------------------------------------------------
   * INDEX.PHP
   *
   * front controller, dispatch to the correct function
   * ----------------------------------------------------------------------
   */

    header('Content-type : text/html ; charset=utf-8');
    error_reporting(E_ALL);
    
    //includes
    $cfg['sys_dir'] = 'system/';
    include($cfg['sys_dir'] . 'config.php');
    include($cfg['sys_dir'] . 'users.php');
    include($cfg['sys_dir'] . 'core.php');
    include($cfg['sys_dir'] . 'helpers.php');
    include($cfg['sys_dir'] . 'wikirenderer.php');
    include($cfg['sys_dir'] . 'plugins.php');
    
    $start = time() + microtime(); //benchmark
    
    //Gets current page, user, language and category
    $pagename        = getPage();
    $cfg['islogged'] = logging();
    $cfg['lang']     = getLang();
    $cfg['cat']      = getCategory($pagename);
    
    //Possible "actions"
    switch (@$_GET['do']) {
        /*
         * Displays the mediamanager + ajax calls
         */
        case 'mediamgr':
            include($cfg['sys_dir'] . 'mediamgr.php');
            break;
        /*
         * Edit the current page if auhtorized
         */
        case 'edit':
            if (pageExists($pagename)) {
                //edit page
                if (isAuthorized("edit", $pagename)) {
                    editPage($pagename);
                } else {
                    if (isAuthorized("read", $pagename)) {
                        showPage($pagename);
                    } else {
                        showPage($cfg['page_default'], "not authorized to edit page");
                    }
                }
                
                
            } else {
                //create page
                if (isAuthorized("create", $pagename)) {
                    editPage($pagename);
                } else {
                    if (isAuthorized("read", $pagename)) {
                        showPage($pagename);
                    } else {
                        showPage($cfg['page_default'], "not authorized to create page");
                    }
                }
                
                
            }
            
            
            break;
        /*
         * Save the edited page if authorized
         */
        case 'save':
            if (pageExists($pagename)) {
                //page edited
                if (isAuthorized("edit", $pagename)) {
                    savePage($pagename);
                    showPage($pagename);
                } else {
                    if (isAuthorized("read", $pagename)) {
                        showPage($pagename);
                    } else {
                        showPage($cfg['page_default'], "not authorized to edit page");
                    }
                }
            } else {
                //page created
                if (isAuthorized("create", $pagename)) {
                    savePage($pagename);
                    showPage($pagename);
                } else {
                    if (isAuthorized("read", $pagename)) {
                        showPage($pagename);
                    } else {
                        showPage($cfg['page_default'], "not authorized to create page");
                    }
                }
            }
            break;
        /*
         * delete a page if authorized
         */
        case 'delete':
            if (isAuthorized("delete", $pagename)) {
                deletePage($pagename);
                if (isAuthorized("read", $pagename)) {
                    showPage($pagename);
                } else {
                    showPage($cfg['page_default'], "not authorized");
                }
            } else {
                showPage($cfg['page_default'], "not authorized");
            }
            
            break;
        /*
         * Displays the current page if authorized
         */          
        case 'show':
        default:
            if (isAuthorized("read", $pagename)) {
                showPage($pagename);
            } else {
                showPage($cfg['page_default'], "not authorized");
            }
            break;
    }
    //shows the benchmark
    //echo time() + microtime() - $start, ' seconds to produce result';
?>
